﻿using System;
using System.Linq;
using System.Web;

namespace Presentation.Web.Core.CDN
{
    internal static class PathHelper
    {
        public static readonly string MediaContentBasePath = "~/Content/";
        public static readonly string ScriptBasePath = "~/Scripts/";

        public static readonly string[] CachableBasePaths = { MediaContentBasePath, ScriptBasePath };

        public static bool IsCachable(string relativePath)
        {
            return GetCachableBasePath(relativePath) != null;
        }

        public static string GetCachableBasePath(string relativePath)
        {
            return CachableBasePaths.FirstOrDefault(path => relativePath.StartsWith(path, StringComparison.OrdinalIgnoreCase));
        }

        public static string Combine(this string basePath, string relativePath)
        {
            return System.IO.Path.Combine(basePath, relativePath).MakeWebPath();
        }

        public static string GetCachedVirtualPath(string basePath, string relativePath, DateTime version)
        {
            string path;
            if (basePath[0] == '~')
            {
                path = ("~/CDN/" + version.ToString("yyyyMMddHHmmss")).Combine(basePath.Substring(2)).Combine(relativePath);
            }
            else
            {
                path = basePath.Combine(version.ToString("yyyyMMddHHmmss")).Combine(relativePath);
            }
            return path;
        }
        internal static string BasePhysicalPath { get; set; }
        public static string GetPhysicalPath(string relativePath)
        {
            HttpContext context = HttpContext.Current;
            if (context == null)
            {
                if (!String.IsNullOrEmpty(BasePhysicalPath))
                {
                    return System.IO.Path.Combine(BasePhysicalPath, relativePath.TrimStart('~', '/'));
                }
                else
                {
                    throw new InvalidOperationException("This method must be invoked under a web context");
                }
            }

            return context.Server.MapPath(relativePath).MakeWindowsPath();
        }

        public static string GetExistingPhysicalPath(string relativePath)
        {
            string filePath = GetPhysicalPath(relativePath);
            return Exists(filePath) ? filePath : null;
        }

        public static bool Exists(string path)
        {
            return System.IO.File.Exists(path) || System.IO.Directory.Exists(path);
        }

        private static string MakeWebPath(this string path)
        {
            return path.Replace(
                System.IO.Path.DirectorySeparatorChar,
                System.IO.Path.AltDirectorySeparatorChar);
        }

        private static string MakeWindowsPath(this string path)
        {
            return path.Replace(
                System.IO.Path.AltDirectorySeparatorChar,
                System.IO.Path.DirectorySeparatorChar);
        }
    }
}
